package misp_backend.config;

import com.aliyun.oss.OSSClient;
import misp_backend.storage.AliyunStorage;
import misp_backend.storage.storage;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//通过配置active来确定是否启用该配置，生成对应的bean。
//注意：如果设置为false，配置没有生效。代码中再注入该bean，如aliyunStorage，就会报错。
@Configuration
@ConditionalOnProperty(prefix = "oss.aliyun", name = "active", havingValue = "true")
@ConditionalOnClass(OSSClient.class)
public class AliyunConfiguration {
    @Bean
    @Qualifier("aliyunStorage")
    storage aliyunStorage(OssProperties ossProperties) {
        OssProperties.Aliyun aliyun = ossProperties.getAliyun();
        OSSClient ossClient = new OSSClient(aliyun.getEndpoint(),
                aliyun.getAccessKeyId(),
                aliyun.getAccessKeySecret());
        return new AliyunStorage(ossClient);
    }
}
